/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.states;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import towerz.game.Sprite;
import towerz.system.BitmapFont;
import towerz.system.StateManager;
import towerz.system.StringManager;
import towerz.system.Utilities;
import towerz.system.xml.XmlNode;
import towerz.system.xml.XmlReaderWriter;

/**
 *
 * @author TimotiusNC
 */
public class StateHelp extends GameState {
	public class Paragraph {
		public Paragraph(XmlNode xml) {
			//Initialize
			m_X		= Integer.parseInt(xml.getAttribute("x"));
			m_Y		= Integer.parseInt(xml.getAttribute("y"));
			m_Lines	= new Vector();
			
			//Read xml
			for (int i = 0; i < xml.getChildren().size(); i++) {
				XmlNode Child = (XmlNode)xml.getChildren().elementAt(i);
				m_Lines.addElement(BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(((XmlNode)Child.getChildren().elementAt(0)).getNodeValue(), 0x00000000));
			}
		}
		
		public void draw(Graphics g) {
			for (int i = 0; i < m_Lines.size(); i++) g.drawImage((Image)m_Lines.elementAt(i), m_X, m_Y + (i * 14), Graphics.TOP | Graphics.LEFT);
		}
		
		protected int		m_X;
		protected int		m_Y;
		protected Vector	m_Lines;
	}
	
	protected static final long ANIMATION_DELAY = 100;

	public StateHelp(){
		super(GameState.HELP, StateManager.instance().getBackgroundImage());

		//Initialize
		m_Time		= 0;
		m_Selected	= 0;
		m_Drawn		= false;
		m_Pages		= new Vector[3];
		m_Pages[0]	= new Vector();
		m_Pages[1]	= null;
		m_Pages[2]	= null;
		m_Images	= new Image[3];
			
		//Load help
		m_XML = XmlReaderWriter.getInstance().readFromXmlFile(Utilities.DATA_FOLDER + "/help_" + StringManager.instance().getTag() + ".xml");
		for (int i = 0; i < ((XmlNode)m_XML.getChildren().elementAt(0)).getChildren().size(); i++)
			m_Pages[0].addElement(new Paragraph((XmlNode)((XmlNode)m_XML.getChildren().elementAt(0)).getChildren().elementAt(i)));
		
		//Create page number
		m_Page = BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("1 / " + m_Pages.length, 0x00000000); 
		
		try {
			//Load images
			m_Arrow			= new Sprite(Utilities.INTERFACE_FOLDER + "/arrow_help.png", 10, 112, 3, 1);
			m_ReturnImage	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("return"), 0x00000000);
			m_Images[0]		= Image.createImage(Utilities.INTERFACE_FOLDER + "/help0.png");
			m_Images[1]		= null;
			m_Images[2]		= null;
		} catch (IOException ex) {}
	}

	public void keyPressed(int key){
		//Super
		super.keyPressed(key);

		if (key == -7 || key == 35) {
			m_Active = false;
			
			//Back sound effect
			StateManager.instance().playSFX("/cancel.wav");
		}
	}

	public void update(long time, int keys) {
		//Super
		super.update(time, keys);
		
		//If already drawn, load the rest of images
		if (m_Drawn && m_Pages[1] == null) {
			//For each reamining page
			for (int i = 1; i < m_Pages.length; i++) {
				//Load help
				m_Pages[i]	= new Vector();
				m_XML = XmlReaderWriter.getInstance().readFromXmlFile(Utilities.DATA_FOLDER + "/help_" + StringManager.instance().getTag() + ".xml");
				for (int j = 0; j < ((XmlNode)m_XML.getChildren().elementAt(i)).getChildren().size(); j++)
					m_Pages[i].addElement(new Paragraph((XmlNode)((XmlNode)m_XML.getChildren().elementAt(i)).getChildren().elementAt(j)));
				
				//Load image
				try {
					m_Images[i] = Image.createImage(Utilities.INTERFACE_FOLDER + "/help" + i + ".png");
				} catch (IOException ex) {}
			}
		} else if (m_Pages[1] != null) {
			//Reset arrow
			if (m_Time > 0) m_Time -= time;
			else {
				m_Time = 0;
				m_Arrow.setFrame(0);
			}
			
			int OldSelection = m_Selected;

			//Check keys
			if ((keys & GameCanvas.LEFT_PRESSED) != 0)	{
				//Previous
				m_Selected--;

				//Animate
				m_Time = ANIMATION_DELAY;
				m_Arrow.setFrame(1);

				//Cursor sound
				StateManager.instance().playSFX("/cursor.wav");
			}

			if ((keys & GameCanvas.RIGHT_PRESSED) != 0 || (keys & GameCanvas.FIRE_PRESSED) != 0)	{
				//Next
				m_Selected++;

				//Animate
				m_Time = ANIMATION_DELAY;
				m_Arrow.setFrame(2);

				//Cursor sound
				StateManager.instance().playSFX("/cursor.wav");
			}

			//Correct selection
			if (m_Selected < 0) m_Selected = m_Pages.length - 1;
			m_Selected = m_Selected % m_Pages.length;
			
			//Create page number
			if (OldSelection != m_Selected) m_Page = BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText((m_Selected + 1) + " / " + m_Pages.length, 0x00000000); 
		}
	}

	public void draw(Graphics g){
		//Super
		super.draw(g);

		//Draw Images
		g.drawImage(StateManager.instance().getDarkImage(), 0, 0, Graphics.LEFT | Graphics.TOP);
		g.drawImage(m_ReturnImage, StateManager.instance().getWidth() - 4, StateManager.instance().getHeight() - 4, Graphics.RIGHT | Graphics.BOTTOM);
		m_Arrow.draw(g);
		
		//Draw
		for (int i = 0; i < m_Pages[m_Selected].size(); i++)
			((Paragraph)m_Pages[m_Selected].elementAt(i)).draw(g);
		g.drawImage(m_Images[m_Selected], StateManager.instance().getWidth(), 0, Graphics.TOP | Graphics.RIGHT);
		g.drawImage(m_Page, StateManager.instance().getWidth() - 18, StateManager.instance().getHeight() - 24, Graphics.BOTTOM | Graphics.RIGHT);
		
		//Drawn
		m_Drawn = true;
	}

	//DATA
	protected boolean	m_Drawn;
	protected long		m_Time;
	protected Sprite	m_Arrow;
	protected Image[]	m_Images;
	protected Vector[]	m_Pages;
	protected Image		m_Page;
	protected int		m_Selected;
	protected Image		m_ReturnImage;
	protected XmlNode	m_XML;
}
